#coding:utf-8
"""
*   TG_Coder : Dzlua
*   Email    : 505544956@qq.com
*   Time     : 2017/08/24
"""

from database import DataBase
import threadpool

import codecs

import os

class Download(DataBase):
    def __init__(self, host, user, pwd, db, threads = 4, task = 10):
        #self.__root = u'/home/zhouhan/dzlua/tg-data'
        self.__root = u'/home/zhouhan/dzlua/tg-data2'

        self.__host = host
        self.__user = user
        self.__pwd = pwd
        self.__db = db

        self.__pool = threadpool.ThreadPool(threads, task)
        DataBase.__init__(self, host, user, pwd, db)
        self.read(self.__on_read)
    def write(self, text):
        f = codecs.open('download' + '.log', 'a', 'utf-8')
        f.write(text)
        f.close()
    def wait(self):
        self.__pool.wait()
    def _select(self, conn, sql):
        cursor = conn.cursor()
        cursor.execute(sql)
        result = cursor.fetchall()
        cursor.close()
        return result
    def __on_read(self, conn, args):
        sql = "select id,type,href,path,name from libdownload where download=0 or download=2;"
        result = self._select(conn, sql)
        for r in result:
            info = {'id': r[0], 'type': r[1], 'href': r[2], 'path': r[3]}

            sql = "select project from libinfo where href='" + r[4] + "';"
            result = self._select(conn, sql)
            for r in result:
                info['project'] = r[0]

            self._downlod(info)
    def __on_update(self, conn, data, args):
        try:
            cursor = conn.cursor()
            sql = "UPDATE libdownload SET download=%s where href=%s;"
            cursor.execute(sql, (data[0], data[1]) )
            cursor.close()
            conn.commit()
        except conn.Error, e:
            err = '[LibDownload] Error __on_update : %d %s, %s ' % (e[0], e[1], data)
            self.write(err + '\n')
            print(err)
    def mkdirs(self, path):
        pt = path[0 : path.rfind(u'/')]
        try:
            os.makedirs(pt.encode('utf8'))
        except:
            print pt
    def put(self, funcall, url, path):
        def fun(url):
            db = DataBase(self.__host, self.__user, self.__pwd, self.__db)
            db.save(self.__on_update, [1, url])
            ret = funcall(url, self.__root + u'/' + path)
            if ret:
                db.save(self.__on_update, [3, url])
            else:
                db.save(self.__on_update, [2, url])
            db.close()
        reqs = threadpool.makeRequests(fun, [url]) 
        [ self.__pool.putRequest(req) for req in reqs ]
    def _downlod(self, info):
        downtype = info['type']
        if downtype == u'zip':
            self.put(self._download_zip, info['href'], info['path'] + u'/' + info['project'] + u'.zip')
        elif downtype == u'tar.gz':
            self.put(self._download_zip, info['href'], info['path'] + u'/' + info['project'] + u'.tar.gz')
        elif downtype == u'git':
            self.put(self._download_git, info['href'], info['path'] + u'/' + info['project'])
    def _download_zip(self, url, path):
        print('[LibDownload] down : type zip, url:%s, path:%s' % (url, path))
        self.mkdirs(path)
        cmd = u"wget '%s' -O %s" % (url, path)
        print cmd
        ret = ( os.system(cmd.encode('utf8')) == 0 )
        
        #
        cmd = u'rm index.html' + url[url.rfind(u'?id='):len(url)]
        print cmd
        os.system(cmd.encode('utf8'))

        return ret
    def _download_git(self, url, path):
        print('[LibDownload] down : type git, url:%s, path:%s' % (url, path))
        cmd = u"git clone '%s' %s" % (url, path)
        return ( os.system(cmd.encode('utf8')) == 0 )
#------------
download = Download('localhost', 'dzlua', 'dzlua', 'spider', 4, 4)
download.wait()
download.close()
print('done')